import com.pinionoffice.Role
import com.pinionoffice.User
import com.pinionoffice.UserRole

class BootStrap {

    def init = { servletContext ->
        def roleUser = Role.findOrCreateWhere(authority: 'ROLE_USER')

        if(!roleUser.id){
            roleUser.save(failOnError: true)
        }

        def roleAdmin = Role.findOrCreateWhere(authority: 'ROLE_ADMIN')

        if(!roleAdmin.id){
            roleAdmin.save(failOnError: true)
        }

        List<User> users = []
        Role.list().each {Role role->
            String name = role.authority.toLowerCase().replace('_', '').replace('role', '')
            10.times {
                User user = new User(username: name + it, password: 'pinion', accountExpired: false, accountLocked: false, enabled: true, passwordExpired: false)
                user.save()
                UserRole.create(user, role, true)
                users << user
            }
        }
    }
    def destroy = {

    }
}
